package telsoft.app.form;

import java.awt.Component;
import javax.swing.JFileChooser;
import java.util.Vector;
import java.io.File;
import java.io.RandomAccessFile;
import java.lang.Object;
import telsoft.util.AppException;
import telsoft.util.StringUtil;
import telsoft.file.util.FileUtil;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: Telsoft</p>
 *
 * @author HuongNV
 * @version 1.0
 */
public class Util
{
    public static String openFile(Component parent)
    {
        JXFileFilter fft = new JXFileFilter();
        fft.addExtension("xls");
        fft.setDescription("File danh sach o dang Excel danh sach");
        JFileChooser jFileOpen = new JFileChooser(System.getProperty("user.dir"));
        jFileOpen.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jFileOpen.setAcceptAllFileFilterUsed(false);
        jFileOpen.setFileFilter(fft);
        jFileOpen.setDialogTitle("Chon File danh sach");
        int result = jFileOpen.showOpenDialog(parent);
        if(result == JFileChooser.APPROVE_OPTION)
        {
            return jFileOpen.getSelectedFile().getAbsolutePath();
        }
        return null;
    }

    ////////////////////////////////////////////////////////////////////////////
    public static void writeLogFile(String strFileName,Vector vtData) throws AppException
    {
        String strContent = "";
        Vector vtRow = new Vector();
        RandomAccessFile fl = null;
        try
        {
            File flDepositOutput = new File(strFileName);
            if(!flDepositOutput.exists())
            {
                if(!flDepositOutput.createNewFile())
                {
                    throw new AppException("can't create file");
                }
            }

            for(int i = 0;i < vtData.size();i++)
            {
                vtRow = (Vector)vtData.get(i);
                strContent += StringUtil.nvl(vtRow.elementAt(0),"") + "\t" + StringUtil.nvl(vtRow.elementAt(1),"") + "\t";
                strContent += StringUtil.nvl(vtRow.elementAt(2),"") + "\t";
		strContent += StringUtil.nvl(vtRow.elementAt(3),"") + "\r\n";
            }

            fl = new RandomAccessFile(strFileName,"rw");
            fl.seek(fl.length());
            fl.write(strContent.getBytes());
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new AppException(e,"Error: writeDeposit()" + e.getMessage());
        }
        finally
        {
            FileUtil.safeClose(fl);
        }
    }
    ////////////////////////////////////////////////////////////////////////////
}
